package org.corolla.cloth.web.action;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.opensymphony.xwork2.ActionSupport;

@ParentPackage(value = "struts-default")
@Results({
          @Result(name = "input", type = "redirect", location = "/view/SizeChoose.jsp"),
          @Result(name = "success", type = "redirect", location = "/view/Lining.jsp") })
@Namespace("/view")
@Action(value = "sizeAction")
public class SizeAction extends ActionSupport {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private String            sizenum;
    private String            checksize;
    private String            checkindex;

    @Override
    public String execute() throws Exception {
        // 程序运行到execute()时，前面的验证都已经正确了
        HttpServletRequest request = ServletActionContext.getRequest();
        HttpSession session = request.getSession();
        boolean access = false;
        Date date = new Date();
        if (date.getHours()>21) {
            access = true;
        }
        if (StringUtils.isNotBlank(this.getSizenum()) && StringUtils.isNotBlank(this.getChecksize())
            && StringUtils.isNotBlank(this.getCheckindex())) {
            String num[] = this.getSizenum().split("_");
            String numsize = "";
            for (int i = 0; i < num.length; i++) {
                if (StringUtils.isNotBlank(num[i])) {
                    if (i < num.length - 1) {
                        numsize = numsize + num[i] + "_";
                    } else {
                        numsize = numsize + num[i];
                    }
                }
            }

            String size[] = this.getChecksize().split("_");
            String sizecheck = "";
            for (int i = 0; i < size.length; i++) {
                if (StringUtils.isNotBlank(size[i])) {
                    if (i < size.length - 1) {
                        sizecheck = sizecheck + size[i] + "_";
                    } else {
                        sizecheck = sizecheck + size[i];
                    }
                }
            }

            String index[] = this.getCheckindex().split("_");
            String indexcheck = "";
            for (int i = 0; i < index.length; i++) {
                if (StringUtils.isNotBlank(index[i])) {
                    if (i < index.length - 1) {
                        indexcheck = indexcheck + index[i] + "_";
                    } else {
                        indexcheck = indexcheck + index[i];
                    }
                }
            }
            session.setAttribute("sizenum", numsize);
            session.setAttribute("checksize", sizecheck);
            session.setAttribute("checkindex", indexcheck);
//            if(access)
                return SUCCESS;
        }
        return "input";
    }

    public String getSizenum() {
        return sizenum;
    }

    public void setSizenum(String sizenum) {
        this.sizenum = sizenum;
    }

    public String getChecksize() {
        return checksize;
    }

    public void setChecksize(String checksize) {
        this.checksize = checksize;
    }

    public String getCheckindex() {
        return checkindex;
    }

    public void setCheckindex(String checkindex) {
        this.checkindex = checkindex;
    }

}
